package com.kaka.bus.engine.home.main.h5;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.di.component.DaggerLoanComponent;
import com.kaka.bus.engine.rechange.GoRechangeActivity;
import com.kaka.bus.engine.result.ResultActivity;
import com.kaka.bus.util.StatusBarUtil;
import com.kaka.bus.util.U;
import com.kaka.bus.util.WindowSoftModeAdjustResizeExecutor;
import com.kaka.bus.view.HeaderBar;

import butterknife.BindView;

/**
 * Created by OnionMac on 2018/8/3.
 */

public class H5Activity extends BaseActivity<H5Presenter> implements H5Contract.View {

    public static final String TYPE = "type";
    public static final String URL = "url";
    public static final String TITLE = "title";
    public static final String TYPE_LOAD_URL = "load_url";
    @BindView(R.id.h5_header)
    HeaderBar mH5Header;
    @BindView(R.id.webview_progressbar)
    ProgressBar mWebviewProgressbar;
    @BindView(R.id.webview)
    WebView mWebview;

    private int mStatus;

    @Override
    protected void initView() {
        super.initView();
        WindowSoftModeAdjustResizeExecutor.assistActivity(this);
        mStatus = StatusBarUtil.StatusBarLightMode(this);

        if (mStatus != 4) {
            mH5Header.setBackgroundColor(Color.WHITE);
        }

        Intent intent = getIntent();

        String title = intent.getStringExtra(TITLE);
        String url = intent.getStringExtra(URL);

        U.initWebView(mWebview, mWebviewProgressbar);


        mWebview.addJavascriptInterface(new JSInterface(), "dc");

        mWebview.loadUrl(url);
    }

    class JSInterface {

        @JavascriptInterface
        public void previouStep(String prodType) {
            runOnUiThread(H5Activity.this::finish);
        }

        @JavascriptInterface
        public void goBackApp(String prodType) {
            runOnUiThread(H5Activity.this::finish);
        }

        @JavascriptInterface
        public void goBackApp() {
            runOnUiThread(H5Activity.this::finish);
        }

        @JavascriptInterface
        public void openAudit(String prodType) {
            runOnUiThread(() -> {
                Intent intent = new Intent(H5Activity.this, ResultActivity.class);
                intent.putExtra(ResultActivity.RESULT_TYPE, ResultActivity.AUDIT);
                startActivity(intent);
                finish();
            });
        }

        @JavascriptInterface
        public void payCost(String prodType, String prodId) {
            int type = 0;
            if (prodType.equals("pay_card")) {
                type = 2;
            }
            Intent intent = new Intent(H5Activity.this, GoRechangeActivity.class);
            intent.putExtra(GoRechangeActivity.TYPE, type);
            intent.putExtra(GoRechangeActivity.PRODUCTID,prodId);
            startActivity(intent);
            finish();
        }

        @JavascriptInterface
        public void loginApp() {
            runOnUiThread(() -> U.tokenOut(H5Activity.this));

        }

        @JavascriptInterface
        public void jdApp(String type) {
            try {
                Intent intent = new Intent();
                intent.setData(Uri.parse("openapp.jdmobile://"));
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(Uri.parse("https://m.jd.com/"));
                startActivity(intent);
            }
        }

        @JavascriptInterface
        public void xyApp(String type) {
            try {
                Intent intent = new Intent();
                intent.setData(Uri.parse("fleamarkets://"));
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.setData(Uri.parse("https://2.taobao.com/"));
                startActivity(intent);
            }

        }

        @JavascriptInterface
        public void callServicePhone(String servicePhone) {
            runOnUiThread(() -> call(servicePhone));
        }
    }

    private void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_h5;
    }

    @Override
    protected void initInject() {
        DaggerLoanComponent.builder().activityComponent(getActivityComponent()).build().inject(this);
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }
}
